using UnityEngine;

public class Weapon : MonoBehaviour
{
    [SerializeField] protected SO_WeaponData weaponData;
    [SerializeField] protected Animator _baseAnimator;
    [SerializeField] protected Animator _weaponAnimator;

    [SerializeField] private int _animHash;
    protected int _attackCounter;
    protected Core _core;

    protected PlayerAttackState _state;

    protected virtual void Awake()
    {
        gameObject.SetActive(false);
        _baseAnimator = transform.Find("Base").GetComponent<Animator>();
        _weaponAnimator = transform.Find("Weapon").GetComponent<Animator>();
    }

    public virtual void EnterWeapon()
    {
        gameObject.SetActive(true);
        print($"{transform.name} enter");

        if (_state.CanContinueAttack)
        {
            if (_attackCounter >= weaponData._amountOfAttack) _attackCounter = 0;
        }
        else
        {
            _attackCounter = 0;
        }

        _animHash = Animator.StringToHash("Attack");
        _baseAnimator.SetBool(_animHash, true);
        _weaponAnimator.SetBool(_animHash, true);

        _animHash = Animator.StringToHash("AttackCount");
        _baseAnimator.SetInteger(_animHash, _attackCounter);
        _weaponAnimator.SetInteger(_animHash, _attackCounter);
    }

    public virtual void ExitWeapon()
    {
        print($"{transform.name} Exit");

        _baseAnimator.SetBool(_animHash, false);
        _weaponAnimator.SetBool(_animHash, false);
        _attackCounter++;

        gameObject.SetActive(false);
    }

    public void Initialize(PlayerAttackState state, Core core)
    {
        _state = state;
        _core = core;
    }

    #region Animation Trigger

    public virtual void AnimationStartMovementTrigger()
    {
        _state.SetPlayerVelocity(weaponData._moventSpeed[_attackCounter]);
    }

    public virtual void AnimationEndMovementTrigger()
    {
        _state.SetPlayerVelocity(0f);
    }

    public virtual void AnimationFinishTrigger()
    {
        _state.AnimationFinshTrigger();
    }

    public virtual void AnimationTurnOffFlipTrigger()
    {
        _state.SetFlipChecck(false);
    }

    public virtual void AnimationTurnOnFlipTrigger()
    {
        _state.SetFlipChecck(true);
    }

    public virtual void AnimationActionTrigger()
    {
    } //���õ���������Ŀ��ʱ��Ķ����߼�

    #endregion
}